package es.abredatos.energyes

import java.util.Calendar;
import java.util.GregorianCalendar;

import groovy.util.GroovyTestCase

class DataServiceTests extends GroovyTestCase {
	
	// Injected by grails
	def dataService
	def dateService
	
	void testParseFiles() {
//		log.info("Testing parseFiles...")
//		
//		String dir = "${System.getProperty('user.dir')}/contrib/data/Noviembre/"
//		
//		log.info("dir: ${dir}")
//		
//		def commercial = new File(dir + 'T_121_1110.txt')
//		def global = new File(dir + 'T_125P_1110.txt')
//		def thermo = new File(dir + 'T_127P_1110.txt')
//		
//		dataService.parseFiles(global.text, thermo.text, commercial.text, 10, 2010)
//		
//		def productionItems = ProductionItem.createCriteria().listDistinct {
//			eq('energy', Energy.NUCLEAR)
//			eq('month', 10)
//			eq('year', 2010)
//		}
//		
//		assertEquals(5327536, productionItems.sum{it.production})
	}
	
	void testGetAndImportMonthlyData() {
//		Calendar calendar = new GregorianCalendar(2010, 10, 20) // the day should be ignored
//		Date date = calendar.time
//		
//		log.info("Testing getAndImportMonthlyData...(date:${date})")
//		
//		dataService.getAndImportMonthlyData(date)
//		
//		def productionItems = ProductionItem.createCriteria().listDistinct {
//			eq('energy', Energy.NUCLEAR)
//			eq('month', 10)
//			eq('year', 2010)
//		}
//		
//		assertEquals(5327536, productionItems.sum{it.production})
	}
	
	void testImportFilesForInterval(){
		def lastYear = dateService.findLastYearWithProductionItems()
		println "Last Year with production items: ${lastYear}"
		def lastMonth = dateService.findLastMonthWithProductionItemsByYear(lastYear)
		println "Last Month with production items: ${lastMonth}"
		Date date = new Date()
		def actualMonth = date[Calendar.MONTH]
		def actualYear = date[Calendar.YEAR]
		println "testImportFilesForInterval - interval: ${lastMonth} ${lastYear} - ${actualMonth} ${actualYear}"
		dataService.importDataForInterval([*lastYear..actualYear], [*lastMonth..actualMonth])
		def productionItems = ProductionItem.createCriteria().listDistinct {
			eq('energy', Energy.NUCLEAR)
			eq('month', 10)
			eq('year', 2010)
		}
		
		assertEquals(5327536, productionItems.sum{it.production})
	}
}